package com.andrew.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.dao.OrderDao;
import com.andrew.dao.impl.OrderDaoImpl;
import com.andrew.entity.Customer;
import com.andrew.entity.Order;
import com.andrew.entity.OrderBeanForAdmin;
import com.andrew.entity.OrderDetail;
import com.andrew.service.BookService;
import com.andrew.util.JDBCUtils;

public class OrderServiceImpl implements com.andrew.service.OrderService {
	private OrderDao orderDao=new OrderDaoImpl();
	private BookService bookService=new BookServiceImpl();

	/**
	 * 添加订单
	 */
	@Override
	public void addOrder(Order order){
		// TODO Auto-generated method stub
		//获取连接对象
		Connection conn=null;
		try {
			conn = JDBCUtils.getConn();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try {
			//开启事务
			conn.setAutoCommit(false);
			//插入订单
			orderDao.addOrder(order, conn);
			//插入订单详情
			orderDao.addOrderDetailList(order.getOrderDetailsList(), conn);
			//提交事务
			conn.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("事务回滚");
			//回滚事务
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		//释放资源
		try {
			conn.setAutoCommit(true);
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	/**
	 * 获取我的订单
	 */
	@Override
	public List<Order> getMyOrders(Customer customer) throws SQLException {
		return orderDao.getOrderByCustomerId(customer);
	}
	/**
	 * 获取所有订单
	 */
	@Override
	public List<Order> getAllOrders() throws SQLException {
		return orderDao.getAllOrders();
	}

	/**
	 * 通过订单号获取订单信息
	 */
	@Override
	public Order getOrderByOrderId(String orderId) throws SQLException {
		// TODO Auto-generated method stub
		return orderDao.getOrdersByOrderId(orderId);
	}

	@Override
	public int updateOrderStatus(String orderId, int status) throws SQLException {
		return orderDao.updateOrderStatus(orderId, status);
	}

	/**
	 * 付款完毕
	 */
	@Override
	public void pay(String orderId) throws SQLException {
		// TODO Auto-generated method stub
		Order order=orderDao.getOrdersByOrderId(orderId);
		
		if (order.getSTATUS()==-1) {
			orderDao.updateOrderStatus(orderId, 0);
		}
		
		//减少库存
		for(OrderDetail orderDetail :order.getOrderDetailsList()) {
			int bookId=orderDetail.getBook().getBookId();
			int quantity=orderDetail.getBook().getStock()-orderDetail.getQuantity();
			bookService.changeStocks(bookId, quantity);
		}
	}

	/**
	 * 收货
	 */
	@Override
	public void receiveGoods(String orderId) throws SQLException {
		// TODO Auto-generated method stub
		orderDao.updateOrderStatus(orderId, 2);
	}

	@Override
	public void setAddress(String orderId,String address) throws SQLException {
		orderDao.setAddress(orderId,address);
	}

	/**
	 * 更新订单详情的书本数量
	 */
	@Override
	public void updateOrderDetail(String orderId, int bookId, int newQuantity,HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Connection con=null;
		try {
			con = JDBCUtils.getConn();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			con.setAutoCommit(false);
			
			if (newQuantity<=0) {
				//要删除这个订单详情
				int orderDetailNum = orderDao.deleteOrderDetail(orderId, bookId, con);
				//如果订单详情删完了
				if (orderDetailNum==0) {
					//删除订单
					orderDao.deleteOrderByOrderId(orderId, con);
				}else {
					//没删完
					//更新总价
					orderDao.updateOrderPrice(orderId, con);
				}
				
			}else {
				//更新订单详情价格和小计
				orderDao.updateOrderDetailQuantityAndsubTotail(orderId, bookId,newQuantity, con);
				//更新总价
				orderDao.updateOrderPrice(orderId, con);
			}
			//成功提交
			con.commit();
			resp.getWriter().write("success");
		} catch (SQLException e) {
			//失败回滚
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			resp.getWriter().write("error");
			e.printStackTrace();
			
		}
		
		//释放资源
		try {
			con.setAutoCommit(true);
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
